package edu.qust.y22.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.qust.y22.bean.Ticket;
import edu.qust.y22.bean.WebModel;
import edu.qust.y22.constant.ErrorEnum;
import edu.qust.y22.constant.Message;
import edu.qust.y22.service.TicketService;

@RestController
@RequestMapping("/bac/ticket")
public class TicketController {
	
	@Autowired
	private TicketService ticketService;
	
	@RequestMapping("selectTicket")
	public Message selectTicket(@RequestBody Ticket ticket) {
		
		if("".equals(ticket.getEnd()) && "".equals(ticket.getStartTime()) &&"".equals(ticket.getStart())) {
			List<Ticket> data = this.ticketService.selectTicket();
			return Message.createSuccessMessage(data);
		}else
		if("".equals(ticket.getEnd()) && "".equals(ticket.getStartTime())) {
			List<Ticket> data = this.ticketService.selectTicket1(ticket.getStart());
			return Message.createSuccessMessage(data);
		}else if("".equals(ticket.getStartTime())) {
			List<Ticket> data = this.ticketService.selectTicket2(ticket.getStart(),ticket.getEnd());
			return Message.createSuccessMessage(data);
		}
		List<Ticket> data = this.ticketService.selectTicket3(ticket.getStart(),ticket.getEnd(),ticket.getStartTime());
		return Message.createSuccessMessage(data);
	}
	
	@RequestMapping("selectTicketB")
	public Message selectTicketB(@RequestBody Ticket ticket) {
		List<Ticket> data = this.ticketService.selectTicket3(ticket.getStart(),ticket.getEnd(),ticket.getStartTime());
		return Message.createSuccessMessage(data);
	}
	
	@RequestMapping("showTicket")
	public Message showTicket(WebModel wm) {
		this.ticketService.selectAll(wm);
		return Message.createSuccessMessage(wm);
	}
	@RequestMapping("updateTicketSum")
	public Message updateTicketSum(@RequestBody Ticket ticket) {
		boolean success = this.ticketService.updateTicketSum(ticket);
		if(success==false) {
			return Message.createFailureMessage(ErrorEnum.UnkonwError);
		}
		return Message.createSuccessMessage();
	}
	@RequestMapping("updateTicket")
	public Message upateTicket(@RequestBody Ticket ticket) {
		boolean sc = this.ticketService.updateTicket(ticket);
		if(sc==true) {
			return Message.createSuccessMessage();
		}
		return Message.createFailureMessage(ErrorEnum.UnkonwError);
	}
	@RequestMapping("deleteTicket")
	public Message deleteTicket(@RequestBody Ticket ticket) {
		boolean sc = this.ticketService.deleteTicket(ticket.getId());
		if(sc==true) {
			return Message.createSuccessMessage();
		}
		return Message.createFailureMessage(ErrorEnum.UnkonwError);
	}
	@RequestMapping("insertTicket")
	public Message insertTicket(@RequestBody Ticket ticket) {
		if(ticket!=null) {
			boolean sc = this.ticketService.addticket(ticket);
			if(sc==true) {
				return Message.createSuccessMessage();
			}
		}
		return Message.createFailureMessage(ErrorEnum.UnkonwError);
	}
}
